package com.dqx.haoqiu.mvp.common.base;

import com.dqx.haoqiu.mvp.R;
import com.dqx.haoqiu.mvp.widget.ErrorDialog;
import com.xmjs.baselibrary.base.IBasePresenter;
import com.xmjs.baselibrary.base.IBaseView;
import com.xmjs.baselibrary.base.activity.lib.LibBaseActivity;
import com.xmjs.baselibrary.base.fragment.lib.LibBaseFragment;
import com.xmjs.baselibrary.utils.ToastUtil;

import javax.inject.Inject;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * 该基类的作用：
 * 1.显示/隐藏错误提示
 */
public class BaseFragment<P extends IBasePresenter> extends LibBaseFragment implements IBaseView,
        ErrorDialog.OnListener {
    @Inject
    public P presenter;

    private ErrorDialog errorDialog;

    /**
     * 显示加载进度视图
     *
     * @param fullScreen
     */
    @Override
    public void showLoadingView(boolean fullScreen) {
        if (!isAdded()) return;
        if (getActivity() instanceof LibBaseActivity)
            ((BaseActivity) getActivity()).showLoadingView(fullScreen);
    }

    /**
     * 隐藏加载进度视图
     */
    @Override
    public void hideLoadingView() {
        if (!isAdded()) return;
        if (getActivity() instanceof LibBaseActivity)
            ((BaseActivity) getActivity()).hideLoadingView();
    }

    /**
     * 重新执行登录
     */
    @Override
    public void onReLogin() {
        if (!isAdded()) return;
        if (getActivity() instanceof LibBaseActivity)
            ((BaseActivity) getActivity()).onReLogin();
    }

    /**
     * 重新加载页面或重新请求接口
     */
    @Override
    public void onReload() {

    }

    /**
     * 吐司
     *
     * @param msg
     */
    @Override
    public void toast(String msg) {
        ToastUtil.getInstance().toast(msg);
    }

    /**
     * 吐司
     *
     * @param msgId
     */
    @Override
    public void toast(int msgId) {
        ToastUtil.getInstance().toast(msgId);
    }

    /**
     * 显示页面错误提示
     *
     * @param imagResId 图片资源id
     * @param errorText 错误文案
     * @param btnText   按钮文案
     */
    @Override
    public void showError(int imagResId, String errorText, String btnText) {
        if (errorDialog != null) {
            errorDialog.dismiss();
            errorDialog = null;
        }
        if (errorDialog == null) {
            errorDialog = new ErrorDialog(getContext())
                    .setImage(imagResId)
                    .setErrorText(errorText)
                    .setBtnVisibility(VISIBLE)
                    .setBtnText(btnText)
                    .setListener(this);
            errorDialog.show();
        }
    }

    /**
     * 显示页面错误提示
     *
     * @param imagResId      图片资源id
     * @param errorResId 错误文案资源id
     * @param btnTextResId   按钮文案资源id
     */
    @Override
    public void showError(int imagResId, int errorResId, int btnTextResId) {
        if (errorDialog != null) {
            errorDialog.dismiss();
            errorDialog = null;
        }
        if (errorDialog == null) {
            errorDialog = new ErrorDialog(getContext())
                    .setImage(imagResId)
                    .setErrorText(getString(errorResId))
                    .setBtnVisibility(VISIBLE)
                    .setBtnText(getString(btnTextResId))
                    .setListener(this);
            errorDialog.show();
        }
    }

    /**
     * 显示页面错误提示
     *
     * @param imagResId    图片资源id
     * @param errorText    错误文案
     * @param btnTextResId 按钮文案资源id
     */
    @Override
    public void showError(int imagResId, String errorText, int btnTextResId) {
        if (errorDialog != null) {
            errorDialog.dismiss();
            errorDialog = null;
        }
        if (errorDialog == null) {
            errorDialog = new ErrorDialog(getContext())
                    .setImage(imagResId)
                    .setErrorText(errorText)
                    .setBtnVisibility(VISIBLE)
                    .setBtnText(getString(btnTextResId))
                    .setListener(this);
            errorDialog.show();
        }
    }

    /**
     * 显示空提示
     *
     * @param isShow    是否显示
     * @param imagResId 图片资源id
     * @param hintText  提示文案
     */
    public void showEmpty(boolean isShow, int imagResId, String hintText) {
        if (errorDialog != null) {
            errorDialog.dismiss();
            errorDialog = null;
        }
        if (errorDialog == null && isShow) {
            errorDialog = new ErrorDialog(getContext())
                    .setImage(imagResId)
                    .setErrorText(hintText)
                    .setBtnVisibility(GONE)
                    .setListener(this);
            errorDialog.show();
        }
    }

    /**
     * 显示空提示
     *
     * @param isShow        是否显示
     * @param imagResId     图片资源id
     * @param hintTextResId 提示文案资源id
     */
    @Override
    public void showEmpty(boolean isShow, int imagResId, int hintTextResId) {
        if (errorDialog != null) {
            errorDialog.dismiss();
            errorDialog = null;
        }
        if (errorDialog == null && isShow) {
            errorDialog = new ErrorDialog(getContext())
                    .setImage(imagResId)
                    .setErrorText(getString(hintTextResId))
                    .setBtnVisibility(GONE)
                    .setListener(this);
            errorDialog.show();
        }
    }

    /**
     * 显示默认的空提示
     *
     * @param isShow 是否显示
     */
    public void showEmpty(boolean isShow) {
        showEmpty(isShow, R.mipmap.ic_me_follow_sebg, getString(R.string.no_data));
    }

}
